﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

using System.Numerics;

using NumDouble = MathNet.Numerics.LinearAlgebra.Double;
using NumComplex = MathNet.Numerics.LinearAlgebra.Complex;

namespace Estimador_EDAL.Estimador
{
    public class CCircuito
    {
        private uint m_BarraDE;
        private uint m_BarraPARA;
        private uint m_NumCIRC;
        private Complex m_Zij;
        private Complex m_Yshij;
        private Complex m_TAPij;

        #region Construção do Objeto

        public CCircuito()
        {
            Defaults();
        }

        public CCircuito(CCircuito circ)
        {
            Copy(circ);
        }

        public CCircuito(uint bDE, uint bPARA, uint nCIRC, Complex z, Complex ysh, Complex tap)
        {
            m_BarraDE = bDE;
            m_BarraPARA = bPARA;
            m_NumCIRC = nCIRC;
            m_Zij = z;
            m_Yshij = ysh;
            m_TAPij = tap;
        }

        private void Defaults()
        {
            m_BarraDE = 0;
            m_BarraPARA = 0;
            m_NumCIRC = 0;
            m_Zij = new Complex();
            m_Yshij = new Complex();
            m_TAPij = new Complex(1.0, 0.0);
        }

        private void Copy(CCircuito circ)
        {
            m_BarraDE = circ.m_BarraDE;
            m_BarraPARA = circ.m_BarraPARA;
            m_NumCIRC = circ.m_NumCIRC;
            m_Zij = circ.m_Zij;
            m_Yshij = circ.m_Yshij;
            m_TAPij = circ.m_TAPij;
        }

        #endregion Construção do Objeto

        #region Get's e Set's

        public uint BarraDE
        {
            get { return m_BarraDE; }
            set { if (m_BarraDE != value) m_BarraDE = value; }
        }

        public uint BarraPARA
        {
            get { return m_BarraPARA; }
            set { if (m_BarraPARA != value) m_BarraPARA = value; }
        }

        public uint NumCIRC
        {
            get { return m_NumCIRC; }
            set { if (m_NumCIRC != value) m_NumCIRC = value; }
        }

        public Complex Zij
        {
            get { return m_Zij; }
            set { if (m_Zij != value) m_Zij = value; }
        }

        public Complex Yshij
        {
            get { return m_Yshij; }
            set { if (m_Yshij != value) m_Yshij = value; }
        }

        public Complex TAPij
        {
            get { return m_TAPij; }
            set { if (m_TAPij != value) m_TAPij = value; }
        }

        #endregion Get's e Set's

        #region Rotinas Públicas - DE e PARA formato texto

        public void CarregaDeTxt(string str)
        {
            string[] str_Partes = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            //validação
            if (str_Partes.Length < 9)
            {
                System.Diagnostics.Debug.WriteLine("Falha (CCircuito.CarregaDeTxt): número de colunas menor que o necessário.");
                return;
            }

            //carrega dados
            CultureInfo formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            try
            {
                m_BarraDE = uint.Parse(str_Partes[0]);
                m_BarraPARA = uint.Parse(str_Partes[1]);
                m_NumCIRC = uint.Parse(str_Partes[2]);

                double real = 0;
                double imag = 0;

                real = double.Parse(str_Partes[3], formatProvider);
                imag = double.Parse(str_Partes[4], formatProvider);
                m_Zij = new Complex(real, imag);

                real = double.Parse(str_Partes[5], formatProvider);
                imag = double.Parse(str_Partes[6], formatProvider);
                m_Yshij = new Complex(real, imag);

                //mod e fase
                real = double.Parse(str_Partes[7], formatProvider);
                imag = double.Parse(str_Partes[8], formatProvider);
                m_TAPij = Complex.FromPolarCoordinates((real == 0.0) ? 1.0 : real, imag);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Falha (CCircuito.CarregaDeTxt): " + e.Message);
            }
        }

        public string DescarregaParaTxt()
        {
            string ret = "";
            CultureInfo formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            ret += m_BarraDE + " ";
            ret += m_BarraPARA + " ";
            ret += m_NumCIRC + " ";
            ret += m_Zij.Real.ToString("0.000", formatProvider) + " ";
            ret += m_Zij.Imaginary.ToString("0.000", formatProvider) + " ";
            ret += m_Yshij.Real.ToString("0.000", formatProvider) + " ";
            ret += m_Yshij.Imaginary.ToString("0.000", formatProvider) + " ";
            ret += m_TAPij.Magnitude.ToString("0.000", formatProvider) + " ";
            ret += m_TAPij.Phase.ToString("0.000", formatProvider);
            return ret;
        }

        #endregion Rotinas Públicas - DE e PARA formato texto
    }
}
